//充值点击事件
function showMask(obj, item) {
	var a = $(obj).attr("data");
	$(".info_mc").attr('data-id', item).html(a);
	//可用余额
	var j = {};
	var adComId = $('.info_mc').attr('data-id');
	j.adComId = Number(adComId);
	$.ajax({
		url: url + "/sys/fiTradeRecordCom/queryUserAccount",
		type: 'POST',
		//		参数需转为字符串
		data: adComId,
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",

		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			console.log(result);
			if(result.code == '0') {

				var data = result.data;
				$(".info_yue1").html(data.amount);

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
	layer.open({
		type: 1,
		title: '充值',
		content: $('.transfer'),
		area: ["580px", "330px"],
		end: function() { //无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
			$(".transfer").css("display", "none");
		}
	})
}
//转账点击事件
function showMaska(obj, item) {
	var a = $(obj).attr("data");
	$(".info_zz").attr('data-id', item).html(a);
	//可用余额
	var j = {};
	var adComId = $('.info_zz').attr('data-id');
	j.adComId = Number(adComId);
	console.log(1);
	$.ajax({
		url: url + "/sys/fiTradeRecordCom/queryUserAccount",
		type: 'POST',
		//		参数需转为字符串
		data: adComId,
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",

		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				var data = result.data;
				$(".info_yue2").html(data.amount);

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});

	layer.open({
		type: 1,
		title: '转账',
		content: $('.transfera'),
		area: ["580px", "330px"],
		end: function() { //无论是确认还是取消，只要层被销毁了，end都会执行，不携带任何参数。layer.open关闭事件
			$(".transfera").css("display", "none");
		}
	})
}
//清除layui弹框
$(document).on("click", ".close-layer", function() {
	layer.closeAll();
});

//	获取地址栏参数
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r != null) return unescape(r[2]);
	return null;
}
var tokena = GetQueryString("token");
var token_id=GetQueryString("id");
var token_str="?token="+token+"&id="+token_id;


//财务信息
var p = 1;

function tiaozhuan(p) {
	var adComName = $(".info_adComName").val();
	var pageSize=$('.ui-select-pagesize').val();
	var d = {};
	d.pageSize = pageSize;
	d.page = p;
	d.adComName = adComName;
	$.ajax({
		url: url + "/sys/fiInfoCom/queryPage",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		contentType: "application/json",
		beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				var data = result.data.rows;
				var html = "";
				for(var i in data) {
					html += '<tr class="ng-scope"><td  class="ng-binding">' + data[i].adComId + '</td>';
					html += '<td  class="ng-binding">' + data[i].adComName + '</td><td  class="ng-binding">' + data[i].balance + '</td>';
					html += '<td  class="ng-binding">' + data[i].totalProfit + '</td><td  class="ng-binding">' + data[i].undrawnProfit + '</td>';
					html += '<td class="ng-binding">' + data[i].drawnProfit + '</td>';
					html += '<td  class="ng-binding"><a  onclick="javascript:mass(this);" data-dd="'+data[i].adComId+'"  style="cursor: pointer">查看记录</a>&nbsp;&nbsp;&nbsp;&nbsp;<a  href="javascript:;" data="' + data[i].adComName + '" onclick="showMask(this,' + data[i].adComId + ')"  class="ng-scope">充值</a>'
					html += '&nbsp;&nbsp;&nbsp;&nbsp;<a  href="javascript:;" onclick="showMaska(this,' + data[i].adComId + ')" data="' + data[i].adComName + '"  class="ng-scope">转出资金</a></td>'
					html += '</tr>';
				}
				$(".info_list").html(html);
				var r=result.data.total;
					paging(p,pageSize,r);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}
tiaozhuan(1);
//下载文件
function download(){
	var param2 = "/sys/fiInfoCom/queryPage";
	var method2 = "post";
	var adComName = $(".info_adComName").val();
	var datas2 = {page: p, pageSize: 1000, adComName: adComName};
	getAgentMsg2(param2,method2,datas2);
	exportCsv({
        title: ['广告主ID','广告公司名称','余额','总利润','未提取利润','已提取利润'],
        titleForKey: ['adComId','adComName','balance','totalProfit','undrawnProfit','drawnProfit'],
        data: dataResult2.data.rows,
    },"财务信息.csv");
}
function mass(d) {
	location.href = "trade_information.html" +token_str+"&acid="+$(d).attr("data-dd");
}

//搜索
$(".info_btn").on("click", function() {
	p = 1;
	tiaozhuan(p);
})

//充值
$(".info_cz").on('click', function() {

	//防止重复点击
	$(this).attr("disabled", "disabled");
	var amount = $('.cz_jine').val();
	var adComId = $('.info_mc').attr('data-id');
	if(!amount) {
		layer.msg("请输入金额");
		$(this).removeAttr("disabled");
		return false;
	}
	var d = {};
	d.amount = amount;
	d.adComId = adComId;
	$.ajax({
		url: url + "/sys/fiInfoCom/rechargeByAdComId",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",

		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg(result.msg, {
					time: 2 * 1000
				}, function() {
					layer.closeAll();
				});

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		},

	});

})
//转账
$(".info_zzh").on('click', function() {
	//防止重复点击
	$(this).attr("disabled", "disabled");
	var amount = $('.zz_jine').val();
	var adComId = $('.info_zz').attr('data-id');
	if(!amount) {
		layer.msg("请输入金额");
		$(this).removeAttr("disabled");
		return false;
	}
	var d = {};
	d.amount = amount;
	d.adComId = adComId;
	$.ajax({
		url: url + "/sys/fiInfoCom/withdrawByAdComId",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",

		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			   
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				layer.msg(result.msg, {
					time: 2 * 1000
				}, function() {
					layer.closeAll();
				});

			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		},
		complete: function() {
			$(_that).removeAttr("disabled");
		}
	});
})